package im.starchat.chat;

import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TabHost;

import com.qiniu.android.storage.UploadManager;
import com.umeng.socialize.PlatformConfig;

import im.starchat.chat.control.QavsdkControl;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.MemberInfo;
import im.starchat.chat.rest.model.UserInfo;
import im.starchat.chat.utils.FileUtils;
import im.starchat.chat.utils.PreferencesStorageUtil;
import im.starchat.chat.utils.Util;


public class XLApplication extends android.support.multidex.MultiDexApplication {
    private static final String TAG = "QavsdkApplication";
    private static QavsdkControl sQavsdkControl = null;
    private static UserInfo sSelfUserInfo;
    private TabHost appTabHost;
    private boolean handleMemberRoomSuccess = false;
    public static int sScreenWidth;
    public static int sScreenHeight;
    private String roomName = "";
    private String roomCoverPath = "";
    private MemberInfo hostInfo;
    public static String sVersionName;
    private static XLApplication mInstance;
    //    public static int sDensityDpi;
    public static float sDensity;
    public static String catchDir;
    public static String cameraDir;


    private RestClient mRestClient;


    public int getEnterIndex() {
        return enterIndex;
    }

    public void setEnterIndex(int enterIndex) {
        this.enterIndex = enterIndex;
    }

    public int getExitIndex() {
        return exitIndex;
    }

    public void setExitIndex(int exitIndex) {
        this.exitIndex = exitIndex;
    }

    private int enterIndex = 0;
    private int exitIndex = 0;
    private String version;

    @Override
    public void onConfigurationChanged(android.content.res.Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "WL_DEBUG onConfigurationChanged");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mInstance = this;
        sQavsdkControl = new QavsdkControl(this);
        sSelfUserInfo = new UserInfo("123", 10, im.starchat.chat.R.drawable.user, 1000);
//        CrashReport.initCrashReport(this, "" + Constants.APPID, true);

        sVersionName = Util.getAppVersionName(this);
        catchDir = new FileUtils(this).makeAppDir();
        cameraDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
                + "/Camera";
        //初始化第三方平台
        PlatformConfig.setWeixin("wxa1e2bb504d49337a", "ee8cae8c065e4c05d1daa51b1f196a7c");
        //微信 appid appsecret
        PlatformConfig.setSinaWeibo("2116142927", "568cc50a10ef6163cf5133e19bfdb990");
        //新浪微博 appkey appsecret
        StringBuilder sb = new StringBuilder();
        sb.append("android-");
        sb.append(android.os.Build.VERSION.RELEASE + "-");
        sb.append(sVersionName);
        version = sb.toString();
        if (PreferencesStorageUtil.isUserLogin(this)) {
            //获取user-agent
            mRestClient = new RestClient(PreferencesStorageUtil.getToken(this), version);
        } else {
            mRestClient = new RestClient("WRc9gGA93dSbsqFAf8VQ", version);
        }

        DisplayMetrics mDisplayMetrics = getResources().getDisplayMetrics();
        sScreenWidth = mDisplayMetrics.widthPixels;
        sScreenHeight = mDisplayMetrics.heightPixels;
//        sDensityDpi = mDisplayMetrics.densityDpi;
        sDensity = mDisplayMetrics.density;

//        com.qiniu.android.storage.Configuration config = new com.qiniu.android.storage.Configuration.Builder()
//                .chunkSize(256 * 1024)  //分片上传时，每片的大小。 默认 256K
//                .putThreshhold(512 * 1024)  // 启用分片上传阀值。默认 512K
//                .connectTimeout(10) // 链接超时。默认 10秒
//                .responseTimeout(60) // 服务器响应超时。默认 60秒
//                .recorder()  // recorder 分片上传时，已上传片记录器。默认 null
//                .recorder(recorder, keyGen)  // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
//                .zone(Zone.zone0) // 设置区域，指定不同区域的上传域名、备用域名、备用IP。默认 Zone.zone0
//                .build();
//        // 重用 uploadManager。一般地，只需要创建一个 uploadManager 对象
        UploadManager uploadManager = new UploadManager();

        Log.d(TAG, "WL_DEBUG onCreate");
    }

    public RestClient getRestClient() {
        return mRestClient;
    }

    public void refreshRestClient(String token) {
        mRestClient = new RestClient(token, version);
    }

    public static XLApplication getInstance() {
        return mInstance;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Log.d(TAG, "WL_DEBUG onLowMemory");
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        Log.d(TAG, "WL_DEBUG onTerminate");
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        Log.d(TAG, "WL_DEBUG onTrimMemory");
    }

    public String getRoomCoverPath() {
        return roomCoverPath;
    }

    public void setRoomCoverPath(String roomCoverPath) {
        this.roomCoverPath = roomCoverPath;
    }

    public String getRoomName() {
        return roomName;
    }

    public void setRoomName(String roomName) {
        this.roomName = roomName;
    }

    public static QavsdkControl getQavsdkControl() {
        if (sQavsdkControl == null) {
            sQavsdkControl = new QavsdkControl(mInstance);
        }
        return sQavsdkControl;
    }

    public static UserInfo getMyselfUserInfo() {
        return sSelfUserInfo;
    }

    public void setAppTabHost(TabHost tabHost) {
        appTabHost = tabHost;
    }

    public TabHost getAppTabHost() {
        return appTabHost;
    }

    public void enterPlusPlus() {
        enterIndex++;
    }

    public void exitPlusPlus() {
        exitIndex++;
    }

    public boolean isHandleMemberRoomSuccess() {
        return handleMemberRoomSuccess;
    }

    public void setHandleMemberRoomSuccess(boolean handleMemberRoomSuccess) {
        this.handleMemberRoomSuccess = handleMemberRoomSuccess;
    }


    public MemberInfo getHostInfo() {
        return hostInfo;
    }

    public void setHostInfo(MemberInfo hostInfo) {
        this.hostInfo = hostInfo;
    }
}